﻿
using System;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SuperGo.SilverlightLib;
using SuperGo.SilverlightLib.Sgf;

namespace SuperGo.TestSilverlightLib.Sgf {
	[TestClass]
	public class SgfWriterTest {
		private SgfWriter _writer;
		private StringWriter _textWriter;

		[TestInitialize]
		public void TestInitialize() {
			_textWriter = new StringWriter();
			MockSystem system;
			SgfWriter.System = system = new MockSystem();
			_writer = new SgfWriter(_textWriter);
			system.CurrentDateTime = new DateTime(2008, 6, 3);
		}

		[TestMethod]
		public void TestWriteEmptyBoard() {
			const string expected = @"
(;GM[1]FF[4]AP[superGo:1.0]ST[1]

SZ[9]KM[0.0]
PW[White]PB[Black]DT[2008-06-03]
)
";
			GoGame game = new GoGame(9, 0, 0.0m, "White", "Black");
			_writer.Write(game);
			_writer.Close();
			Assert.AreEqual(FixSgf(expected), FixSgf(_textWriter.ToString()));
		}

		[TestMethod]
		public void TestWriteSimple() {
			const string expected = @"
(;GM[1]FF[4]AP[superGo:1.0]ST[1]

SZ[9]KM[0.0]
PW[White]PB[Black]DT[2008-06-03]

;B[bb]
;W[cb]
;B[cc]
;W[bc]
;B[tt]
;W[tt] )
";
			GoGame game = new GoGame(9, 0, 0.0m, "White", "Black");
			game.Play(1, 1);
			game.Play(2, 1);
			game.Play(2, 2);
			game.Play(1, 2);
			game.Pass();
			game.Pass();
			_writer.Write(game);
			_writer.Close();
			Assert.AreEqual(FixSgf(expected), FixSgf(_textWriter.ToString()));
		}

		private string FixSgf(string sgf) {
			sgf = sgf.Replace("\r", " ");
			sgf = sgf.Replace("\n", " ");
			sgf = Regex.Replace(sgf, @"\s+", " ");
			sgf = sgf.Trim();
			return sgf;
		}
	}
}
